package com.kd.crawler.dao;

import java.util.List;

import com.kd.crawler.entity.CrawlerEntry;

public class OriginalHtmlDao extends BaseDao {

	private static final OriginalHtmlDao instance = new OriginalHtmlDao();
	
	private OriginalHtmlDao() {
		
	}
	
	public static OriginalHtmlDao getInstance() {
		return instance;
	}
	
	public int insert(String urlMd5, String url, String html) {
		String sql = "insert into tb_original_html(url_md5, url, html_text, create_time) values(?, ?, ?, now())";
		try {
			return this.update(sql, urlMd5, url, html);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	
	public int update(int id, String isParsed) {
		String sql = "update tb_original_html set is_parsed=? where id = ?";
		try {
			return this.update(sql, isParsed, id);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	public List<CrawlerEntry> query(int start, int limit) {
		String sql = "select id, entry_id entryId, job_id jobId, parser_name parserName, html_text html, url from tb_original_html " +
				" where status_code=200 and is_parsed='N' order by create_time limit ?, ?";
		return this.query(sql, CrawlerEntry.class, start, limit);
	}
	
	public static void main(String[] args) {
//		OriginalHtmlDao.getInstance().insert("123", "123124", "html");
		List<CrawlerEntry> list = OriginalHtmlDao.getInstance().query(0, 1);
		System.out.println(list.get(0).getHtml());
	}

}
